/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "novel_test.h"

#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_font.h>

void NovelTest::OnTestPerformance()
{
    OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
    OH_Drawing_BrushSetColor(brush, FONT_COLOR);
    OH_Drawing_CanvasAttachBrush(canvas_, brush);
    OH_Drawing_Font* font = OH_Drawing_FontCreate();
    OH_Drawing_FontSetTextSize(font, fontSize_);
    float curY = START_Y;
    for (size_t i = 0; i < text_.size(); i++) {
        const char* str = text_[i];
        float curX = START_X + spaceWidth_;
        size_t len = strlen(str);
        size_t currentOffset = 0;
        while (currentOffset < len) {
            size_t charLength = GetUtf8CharLength(str + currentOffset);
            float textWidth = 0;
            OH_Drawing_FontMeasureSingleCharacter(font, str + currentOffset, &textWidth);
            OH_Drawing_CanvasDrawSingleCharacter(canvas_, str + currentOffset, font, curX, curY);
            curX += textWidth;
            if (curX > lineWidth_) {
                curX = START_X;
                curY += lineHeight_;        
            }
            currentOffset += charLength;
        }
        curY += lineHeight_;
    }
    OH_Drawing_CanvasDetachBrush(canvas_);
    OH_Drawing_FontDestroy(font);
    OH_Drawing_BrushDestroy(brush);
}